﻿using System;
using System.ComponentModel;
using CommonLibrary;
using InputManager.InputHandlers;
using InputManager.View;

namespace InputManager
{
    public class RawInputManager
    {
        #region Proprieties
        private InputWindow _inputWindow;
        private readonly InputHandler _inputProcess;
        
        public event EventHandler IsClosing;
        #endregion

        #region Constructor
        public RawInputManager(IAppContext context)
        {
            _inputProcess = new InputHandler(context);
        }
        #endregion

        #region Start / Stop Input Capture and State
        public void StartInputCapture()
        {
            if (_inputWindow != null) return;

            _inputWindow = new InputWindow(35); // End Key
            _inputWindow.Closing += InputWindowClosing;
            _inputWindow.Show();

            _inputProcess.Start(_inputWindow);
        }

        private void InputWindowClosing(object sender, CancelEventArgs eventArgs)
        {
            try
            {
                IsClosing.Invoke(sender, eventArgs);
            }
            catch {}
        }

        public void StopInputCapture()
        {
            if (_inputWindow == null) return;

            try { 
                _inputWindow.Close(); 
            }
            catch { }

            _inputWindow = null;
        }
        #endregion
    }
}
